/**
 * @description 公共使用的方法
 * @author clai
 * @time 2020/09/18
 */


var local = window.localStorage
/**
 * @description 设置、获取、删除 localStorage
 * @author clai
 * @time 2020/09/18
 * 
 * 
 * `set(key, value)`: void
 * 
 * `get(key)`: {msg}
 * 
 * `remove(key)`: void
 */
class Local {
    static set(key, val) {
        local?.setItem(key, JSON.stringify(val)) 
    }
    static get(key) {
        return {
            msg: JSON.parse(local?.getItem(key)) || "" 
        }
    }
    static remove(key) {
        local?.removeItem(key)
    }
    static removeAll() {
        local?.clear()
    }
}


/**
 * @description 防抖、节流
 * @author clai
 * @time 20/09/20
 */
const utils = (function() {
    return {
        debounce(fn, timeout=1000) {
            let timeId = null
            return function() {
                if(timeId) clearTimeout(timeId);
                timeId = setTimeout(() => {
                    // console.log(this)
                    fn.call(this, arguments)
                }, timeout)
            }
        },
        throttle(fn, timeout=1000) {
            let pre = 0
            return function() {
                let nowt = ~~Date.now()
                if(nowt - pre > timeout) {
                    fn.call(this, arguments)
                    pre = nowt
                }
            }
        },
        addZero(val) {
            return Number(val) >= 10 ? ''+val : `0${val}`;
        }
    }
})();

export {
    Local, utils
}
